<?php

namespace App\Listeners;

use App\Enums\AvailableStatus;
use App\Enums\OrderStatus;
use App\Events\OrderPaid;
use App\Events\UserAuthed;
use App\Events\UserRegistered;
use App\Models\FrontendUser;
use App\Models\InvitationActivity;
use App\Models\InvitationRegister;
use Illuminate\Contracts\Queue\ShouldQueue;

class InviteCountSubscriber implements ShouldQueue
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
    }

    public function handleUserRegistered(UserRegistered $event)
    {
        $user = $event->user;
        [$activity, $register] = $this->findActivityRegister($event->user);
        if ($activity) {
            //查询在活动期间注册的用户数量
            $inviteCount = FrontendUser::query()
                ->where('parent_id', $user->parent_id)
                ->where('created_at', '>=', $activity->start_time)
                ->where('created_at', '<=', $activity->end_time)
                ->count();
            $register->register_count = $inviteCount;
            $register->save();
        }
    }

    public function handleUserAuthed(UserAuthed $event)
    {
        $user = $event->user;
        [$activity, $register] = $this->findActivityRegister($event->user);
        if ($activity && 0 == $activity->enable_consume) {
            // 查询在活动期间注册的已实名的用户数量
            $authCount = FrontendUser::query()
                ->whereHas('auth')
                ->where('parent_id', $user->parent_id)
                ->where('created_at', '>=', $activity->start_time)
                ->where('created_at', '<=', $activity->end_time)
                ->count();
            $register->realname_count = $authCount;
            $register->save();
        }
    }

    public function handleUserOrder(OrderPaid $event)
    {
        $user = $event->order->user;
        [$activity, $register] = $this->findActivityRegister($user);
        if ($activity && 1 == $activity->enable_consume) {
            // 查询在活动期间下单的用户数量
            $userCount = FrontendUser::query()
                ->whereHas('order', function ($w) {
                    return $w->where('order_status', OrderStatus::PAID);
                })
                ->where('parent_id', $user->parent_id)
                ->where('created_at', '>=', $activity->start_time)
                ->where('created_at', '<=', $activity->end_time)
                ->count();
            $register->realname_count = $userCount;
            $register->save();
        }
    }

    public function subscribe($events)
    {
        $className = self::class;
        $events->listen(UserRegistered::class, "{$className}@handleUserRegistered");
        $events->listen(UserAuthed::class, "{$className}@handleUserAuthed");
        $events->listen(OrderPaid::class, "{$className}@handleUserOrder");
    }

    private function findActivityRegister(FrontendUser $user)
    {
        if (!$user->parent_id) {
            return null;
        }
        $inviteId = $user->parent_id;
        /** @var InvitationActivity $activity */
        $activity = InvitationActivity::query()
            ->where('status', AvailableStatus::ENABLED)
            ->where('agent_id', $user->agent_id)
            ->where('start_time', '<=', $user->created_at)
            ->where('end_time', '>=', $user->created_at)
            ->first();
        if (!$activity) {
            return null;
        }
        $register = $activity->registers()->where('invite_id', $inviteId)->first();
        if (!$register) {
            $register = new InvitationRegister();
            $register->activity_id = $activity->id;
            $register->invite_id = $inviteId;
        }

        return [$activity, $register];
    }
}
